/**
 * author: zhangwei
 * 角色新增、编辑
 */
var pageType = "add";
var score = 0;

var main = {

    init: function() {

        pageType = localStorage.taskType;
        this.id = localStorage.id;
        $("#id").val(this.id)
        loadData(this.id);
        this.initTable();
    },
    initTable: function() {
        layui.use(['form','laydate'], function() {
            var form = layui.form;
            laydate = layui.laydate;
            laydate.render({
                elem: '#startTime'
            });
            laydate.render({
                elem: '#endTime'
            });
            //监听提交
            form.on('submit(formSubmit)', function(data) {
                var url = "/task/evaluate";
                var id = $("#id").val();
                var evaluate = $("#evaluate").val();
                var score = $("#score").val()
                var json = {'id':localStorage.id,'evaluate':evaluate,'score':score}
                $.ajax({
                    type:"post",
                    data:json,
                    async:false,
                    url:url,
                    success:function(result) {
                        if (result.code == property.successCode) {
                            successMsg("操作成功");
                            parent.$t.goback("/task/taskComplete");
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error:function(result) {
                        errorMsg("系统异常");
                    }
                });
                return false;
            });

            //监听重置
            $("#cancel").click(function () {
                    parent.$(".myRefresh").click();
                    layer.close(index);
                    return false;
            });

        });
    }
}
main.init();

/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData(id) {
    this.type = "edit";
    layui.use(['form','rate'], function(){
        var form = layui.form;
        var rate = layui.rate;
        var index = parent.layer.getFrameIndex(window.name);
        // rate.render({
        //     elem: '#score'
        //     ,choose: function(value){
        //         score = value
        //     }
        // });
        var json = {"id":id};
        //加载数据
        $.ajax({
            type:"get",
            data:json,
            async:false,
            url:"/task/getTaskComplete",
            success:function(result) {
                if (result.code == property.successCode) {
                    setFormData(result.data);
                    // form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });
}

function loadJob() {
    $.ajax({
        type:"get",
        async:false,
        url:"/job/queryAll",
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data
                setJob(data)
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
}

function setJob(data) {
    htmlstr = ""
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item['id']+">"+item['jobName']+"</option>"
        htmlstr = htmlstr+str
    }
    $("#jobId").html(htmlstr)
}

/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    property.setForm($("#taskForm"),data);
    var userType = data.userType;
    if(userType != 'company'){
        $("#score").attr("disabled","disabled")
        $("#evaluate").attr("disabled","disabled")
        $("#evaluateDiv").hide()
    }

}



